package logdog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogDog {

  private static final Pattern patternExceition = Pattern.compile("^\\S.*?Exception");


  private static String logFileName = "c:\\cuci\\dog.log";
  public static void main(String[] args) {
    snoop();
  }

  public static void snoop() {
    SortedMap<String, Integer> mapExceition = new TreeMap<String, Integer>();
    File logfile = new File(logFileName);
    InputStream is = null;
    BufferedReader reader = null;
    try {
      is = new FileInputStream(logfile);
      reader = new BufferedReader(new InputStreamReader(is));
      while (reader.ready()) {
        String line = reader.readLine();
        Matcher matcherExceition = patternExceition.matcher(line);
        if (matcherExceition.find()) {
          Integer nr = mapExceition.get(line);
          if (nr == null) {
            nr = 0;
          }
          mapExceition.put(line, nr + 1);
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    Iterator<String> iterator = mapExceition.keySet().iterator();
    while (iterator.hasNext()) {
      String key = (String) iterator.next();
      System.out.println(key + " [" + mapExceition.get(key) + "]");
    }
  }
}
